/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatWindow.java
 *
 * Created on 19/06/2009, 00:29:52
 */
package mrstalk;

import javax.swing.JTextArea;

/**
 *
 * @author gabriel
 */
public class ChatWindow extends javax.swing.JDialog {

    // 0 = Running
    // 1 = Trying to stop
    // 2 = Stopped
    private int receiverState = 0;

    public boolean isOpen()
    {
        boolean ret = true;

        if (receiverState != 0)
        {
            receiverState = 2;
            ret = false;
        }

        return ret;
    }

    public void updateMsgBox(String msg) {
        JTextArea talk = txtTalk;
        talk.setText(talk.getText() + "\r\n" + msg);
    }

    /** Creates new form ChatWindow */
    public ChatWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(btnSend);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSend = new javax.swing.JButton();
        sclTalk = new javax.swing.JScrollPane();
        txtTalk = new javax.swing.JTextArea();
        txtMessage = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mrstalk.MRSTalkApp.class).getContext().getResourceMap(ChatWindow.class);
        btnSend.setText(resourceMap.getString("btnSend.text")); // NOI18N
        btnSend.setName("btnSend"); // NOI18N
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        sclTalk.setName("sclTalk"); // NOI18N

        txtTalk.setColumns(20);
        txtTalk.setEditable(false);
        txtTalk.setLineWrap(true);
        txtTalk.setRows(11);
        txtTalk.setName("txtTalk"); // NOI18N
        sclTalk.setViewportView(txtTalk);

        txtMessage.setText(resourceMap.getString("txtMessage.text")); // NOI18N
        txtMessage.setName("txtMessage"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sclTalk, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                        .addGap(10, 10, 10)
                        .addComponent(btnSend)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sclTalk, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSend))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    }//GEN-LAST:event_formWindowActivated

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        String msg = txtMessage.getText();
        Chat chat = MRSTalkApp.getChat();

        msg = chat.sendMsg(msg);
        updateMsgBox(msg);
        txtMessage.setText("");

    }//GEN-LAST:event_btnSendActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        receiverState = 1;

        while (receiverState != 2)
        {
        }
        
        Chat chat = MRSTalkApp.getChat();
        chat.closeConnection();
    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChatWindow dialog = new ChatWindow(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JScrollPane sclTalk;
    private javax.swing.JTextField txtMessage;
    private javax.swing.JTextArea txtTalk;
    // End of variables declaration//GEN-END:variables
}
